﻿using System;
using System.Collections.Generic;
using System.Linq;
using Loki.Engine;
using Luna.Data.Accounting;
using NUnit.Framework;

namespace Luna.Data.Tests
{
    public class CreditCardTest : AssertionHelper
    {
        public IAccountCreditCardData Card1 { get; set; }
        public IAccountCreditCardData Card2 { get; set; }
        public IAccountCreditCardData Card3 { get; set; }

        public IAccountData MainAccount { get; set; }

        private bool AreSame(IAccountCreditCardData P_Op1, IAccountCreditCardData P_Op2)
        {
            bool b = true;
            b &= P_Op1.PK == P_Op2.PK;
            b &= P_Op1.AccountID == P_Op2.AccountID;
            b &= P_Op1.Number == P_Op2.Number;
            b &= P_Op1.Security == P_Op2.Security;
            b &= P_Op1.Type == P_Op2.Type;
            b &= P_Op1.UserName == P_Op2.UserName;
            b &= P_Op1.Validity == P_Op2.Validity;
            return b;
        }

        public IAccountingDataProvider Data { get; set; }

        private IAccountData CreateAccount(string P_Name)
        {
            IAccountData L_Return = Data.Create<IAccountData>();
            L_Return.PK = Guid.NewGuid();
            L_Return.Number = P_Name + "Number";
            L_Return.Name = P_Name + "Name";
            L_Return.Bank = P_Name + "Bank";
            L_Return.InitialAmount = P_Name.Length;
            L_Return.StartDate = DateTime.Today.AddDays(P_Name.Length);
            return L_Return;
        }

        private IAccountCreditCardData Create(string P_Name, Guid P_AccountID)
        {
            IAccountCreditCardData L_Return = Data.Create<IAccountCreditCardData>();
            L_Return.PK = Guid.NewGuid();
            L_Return.AccountID = P_AccountID;
            L_Return.Number = P_Name + "Number";
            L_Return.Security = P_Name + "Security";
            L_Return.Type = P_Name + "Type";
            L_Return.UserName = P_Name + "UserName";
            L_Return.Validity = new DateTime(1980, 01, 14);
            return L_Return;
        }

        [SetUp]
        public void SetUp()
        {
            Data = ContextHelper.GetDataModel<LunaDataModel>().Accounting;

            MainAccount = CreateAccount("Account");

            Card1 = Create("Operation1", MainAccount.PK);
            Card2 = Create("Operation2", MainAccount.PK);
            Card3 = Create("Operation3", MainAccount.PK);

            CleanTable();
        }

        [TearDown]
        public void CleanTable()
        {
            IEnumerable<IAccountCreditCardData> L_Operations = Data.GetAllCreditCards().Where(x => x.Validity == new DateTime(1980, 01, 14));
            foreach (var L_Op in L_Operations)
            {
                Data.DeleteCreditCard(L_Op.PK);
            }

            foreach (var L_Op in L_Operations)
            {
                Data.DeleteAccount(L_Op.AccountID);
            }
        }

        [Test]
        public void TestGetAllByAccount()
        {
            Data.SaveAccount(MainAccount);

            Data.SaveCreditCard(Card1);

            IEnumerable<IAccountCreditCardData> L_Operations = Data.GetAllCreditCardsByAccount(MainAccount.PK);
            Expect(L_Operations.Count(), EqualTo(1));
        }

        [Test]
        public void TestInsertAndGet()
        {
            Data.SaveAccount(MainAccount);

            Data.SaveCreditCard(Card1);
            IAccountCreditCardData L_Operation = Data.GetCreditCard(Card1.PK);
            Expect(AreSame(Card1, L_Operation), Is.True);
        }

        [Test]
        public void TestUpdateAndGet()
        {
            Data.SaveAccount(MainAccount);

            Data.SaveCreditCard(Card1);
            Card1.Number = "Operation1_Name_Modified";
            Data.SaveCreditCard(Card1);

            IAccountCreditCardData L_Operation = Data.GetCreditCard(Card1.PK);
            Expect(AreSame(Card1, L_Operation), Is.True);

            Expect(L_Operation.Number, Is.EqualTo("Operation1_Name_Modified"));
        }

        [Test]
        public void TestInsertAndGetAll()
        {
            Data.SaveAccount(MainAccount);

            Data.SaveCreditCard(Card1);
            Data.SaveCreditCard(Card2);
            Data.SaveCreditCard(Card3);

            int L_Count = Data.GetAllCreditCards().Count();
            Expect(L_Count, Is.EqualTo(3));
        }

        [Test]
        public void TestInsertAndDelete()
        {
            Data.SaveAccount(MainAccount);

            Data.SaveCreditCard(Card1);
            Data.SaveCreditCard(Card2);
            Data.DeleteCreditCard(Card1.PK);

            int L_Count = Data.GetAllCreditCards().Count();
            Expect(L_Count, Is.EqualTo(1));
            IOtherOperationData L_Account = Data.GetOtherOperation(Card1.PK);
            Expect(L_Account, Is.Null);
        }

        [Test]
        public void TestNoAccount()
        {
            Expect(() => Data.SaveCreditCard(Card1), Throws.Exception);
        }
    }
}
